﻿using ElsProp.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TlToolBase;

namespace ElsProp
{
    /// <summary>
    /// FristWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FristWindow : Window
    {
        FristWindowModel mode { get; set; }
        List<zhuangbeilan> zhuangbeilans { get; set; }

        /// <summary>
        /// 启动序号
        /// </summary>
        public int index { get; set; } = 0;
        /// <summary>
        /// 用户数据
        /// </summary>
        public UserData UserData { get; set; }
        /// <summary>
        /// 是否新页
        /// </summary>
        public bool IsNewPage { get; set; }
        public FristWindow()
        {
            InitializeComponent();
            mode = this.DataContext as FristWindowModel;
            this.Loaded += FristWindow_Loaded;
            zhuangbeilans = new List<zhuangbeilan>();
        }

        private void FristWindow_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in zuo.Children)
            {
                if (item is zhuangbeilan)
                {
                    zhuangbeilans.Add((zhuangbeilan)item);
                }
            }
            foreach (var item in you.Children)
            {
                if (item is zhuangbeilan)
                {
                    zhuangbeilans.Add((zhuangbeilan)item);
                }
            }
            foreach (var item in zhuangbeilans)
            {
                item.change += Item_change;
            }
            if (UserData == null)
            {
                UserData = FileDataClass.Get<UserData>();
            }
            if (UserData.Pros != null && UserData.Pros.Count > 0)
            {
                if (index == 0)
                {
                    for (Int32 i = 1; i < UserData.Pros.Count; i++)
                    {
                        FristWindow frist = new FristWindow();
                        frist.index = i;
                        frist.UserData = UserData;
                        frist.Show();
                    }
                }
                foreach (var item in zhuangbeilans)
                {
                    var pro = UserData.Pros[index].propModels.FindAll(x => x.BuWei == item.zhuangBeiLanType);
                    if (pro != null)
                    {
                        if (pro.Count>1)
                        {
                           var zbl= zhuangbeilans.FindAll(x => x.zhuangBeiLanType == item.zhuangBeiLanType);
                            if (zbl.Count>1)
                            {
                                for (Int32 i = 0; i < pro.Count; i++)
                                {
                                    if (zbl[i].mode.propModels==null)
                                    {
                                        zbl[i].mode.propModels = pro[i];
                                        if (zbl[i].mode.propModels.SuitProperties == null || zbl[i].mode.propModels.SuitProperties.Count == 0)
                                        {
                                            var data = mode.propModels.FindAll(x => x.Suit == zbl[i].mode.propModels.Suit && x.SuitName == zbl[i].mode.propModels.SuitName);
                                            foreach (var da in data)
                                            {
                                                if (da != null && da.SuitProperties != null && da.SuitProperties.Count > 0)
                                                {
                                                    zbl[i].mode.propModels.SuitProperties = da.SuitProperties;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            foreach (var item2 in pro)
                            {
                                if (item.mode.propModels == null)
                                {
                                    item.mode.propModels = item2;
                                    if (item.mode.propModels.SuitProperties == null || item.mode.propModels.SuitProperties.Count == 0)
                                    {
                                        var data = mode.propModels.FindAll(x => x.Suit == item.mode.propModels.Suit && x.SuitName == item.mode.propModels.SuitName);
                                        foreach (var da in data)
                                        {
                                            if (da != null && da.SuitProperties != null && da.SuitProperties.Count > 0)
                                            {
                                                item.mode.propModels.SuitProperties = da.SuitProperties;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                       
                }
                Item_change(null);
            }
            if (IsNewPage)
            {
                if (UserData.Pros == null)
                {
                    index = 1;
                }
                else if (UserData.Pros.Count > 0)
                {
                    index = UserData.Pros.Count;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        private void Item_change(zhuangbeilanmode obj)
        {
            List<PropModel> props = new List<PropModel>();
            foreach (var item in zhuangbeilans)
            {
                if (item.mode.propModels != null)
                {
                    props.Add(item.mode.propModels);
                }
            }
            mode.SubSuit(props);
           // mode.SubPropModel(props);
        }

        private void Button_Click_OpenNewPage(object sender, RoutedEventArgs e)
        {
            FristWindow frist = new FristWindow();
            frist.IsNewPage = true;
            frist.Show();
        }

        private void Button_Click_Edit(object sender, RoutedEventArgs e)
        {
            zhuangBeiBianJi shuxingbianji = new();
            shuxingbianji.Show();
            //OpenDirectory("FileData");
        }
        /// <summary>
        /// 打开目录
        /// </summary>
        public static void OpenDirectory(string path)
        {
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = path;
            process.StartInfo = psi;
            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                process?.Close();

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_SaveUserData(object sender, RoutedEventArgs e)
        {
            if (UserData.Pros == null)
            {
                UserData.Pros = new List<SavePros>();
            }
            if (UserData.Pros.Count > index)
            {
                UserData.Pros.RemoveAt(index);
            }
            SavePros savePros = new SavePros();
            savePros.propModels = new List<PropModel>();
            foreach (var item in zhuangbeilans)
            {
                if (item.mode.propModels != null)
                    savePros.propModels.Add(item.mode.propModels);
            }
            UserData.Pros.Add(savePros);
            FileDataClass.Save(UserData);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (UserData.Pros == null)
            {
                UserData.Pros = new List<SavePros>();
            }
            if (UserData.Pros.Count > index)
            {
                UserData.Pros.RemoveAt(index);
            }
            FileDataClass.Save(UserData);
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mode == null)
            {
                return;
            }
            mode.SelectPro();
        }

        private void zhuangBeiList_RightClick(object obj)
        {
            var data = obj as PropModel;
            if (data != null)
            {
                var zb = zhuangbeilans.Find(x => x.zhuangBeiLanType == data.BuWei);
                if (zb != null)
                    zb.mode.propModels = data;
                Item_change(null);
            }

        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            Item_change(null);
        }
    }
}
